package com.river.springbootdemo.thinkingjava.innerclass.controller;

import java.util.ArrayList;
import java.util.List;

/**
 * the reusable framework for control Systems
 */
public class Controller {
    private List<Event> eventList = new ArrayList<>();

    public void addEvent(Event e) {
        if (e != null) {
            eventList.add(e);
        }
    }

    public void run() {
        while (eventList.size() > 0) {
            //make a copy so you are not modifying the list and prevention of ConcurrentModificationException
            //while you are selecting the elements in it
            for (Event e : new ArrayList<>(eventList)) {
                if (e.ready()) {
                    System.out.println(e);
                    e.action();
                    eventList.remove(e);
                }
            }
        }
    }
}
